home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / make-367.lha / make-3.67 / rule.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  15KB  |  529 lines

  1. /* Pattern and suffix rule internals for GNU Make.
  2. Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "rule.h"
  25.  
  26. static void freerule ();
  27.  
  28. /* Chain of all pattern rules.  */
  29.  
  30. struct rule *pattern_rules;
  31.  
  32. /* Pointer to last rule in the chain, so we can add onto the end.  */
  33.  
  34. struct rule *last_pattern_rule;
  35.  
  36. /* Number of rules in the chain.  */
  37.  
  38. unsigned int num_pattern_rules;
  39.  
  40. /* Maximum number of dependencies of any pattern rule.  */
  41.  
  42. unsigned int max_pattern_deps;
  43.  
  44. /* Maximum length of the name of a dependencies of any pattern rule.  */
  45.  
  46. unsigned int max_pattern_dep_length;
  47.  
  48. /* Pointer to structure for the file .SUFFIXES
  49.    whose dependencies are the suffixes to be searched.  */
  50.  
  51. struct file *suffix_file;
  52.  
  53. /* Maximum length of a suffix.  */
  54.  
  55. unsigned int maxsuffix;
  56.  
  57. /* Compute the maximum dependency length and maximum number of
  58.    dependencies of all implicit rules.  Also sets the subdir
  59.    flag for a rule when appropriate, possibly removing the rule
  60.    completely when appropriate.  */
  61.  
  62. void
  63. count_implicit_rule_limits ()
  64. {
  65.   char *name;
  66.   unsigned int namelen;
  67.   register struct rule *rule, *lastrule;
  68.  
  69.   num_pattern_rules = 0;
  70.   
  71.   name = 0;
  72.   namelen = 0;
  73.   rule = pattern_rules;
  74.   lastrule = 0;
  75.   while (rule != 0)
  76.     {
  77.       unsigned int ndeps = 0;
  78.       register struct dep *dep;
  79.       struct rule *next = rule->next;
  80.     
  81.       ++num_pattern_rules;
  82.       
  83.       for (dep = rule->deps; dep != 0; dep = dep->next)
  84.     {
  85.       unsigned int len = strlen (dep->name);
  86.       char *p = rindex (dep->name, '/');
  87.       char *p2 = p != 0 ? index (dep->name, '%') : 0;
  88.  
  89.       ndeps++;
  90.  
  91.       if (len > max_pattern_dep_length)
  92.         max_pattern_dep_length = len;
  93.  
  94.       if (p != 0 && p2 > p)
  95.         {
  96.           /* There is a slash before the % in the dep name.
  97.          Extract the directory name.  */
  98.           if (p == dep->name)
  99.         ++p;
  100.           if (p - dep->name > namelen)
  101.         {
  102.           if (name != 0)
  103.             free (name);
  104.           namelen = p - dep->name;
  105.           name = (char *) xmalloc (namelen + 1);
  106.         }
  107.           bcopy (dep->name, name, p - dep->name);
  108.           name[p - dep->name] = '\0';
  109.  
  110.           /* In the deps of an implicit rule the `changed' flag
  111.          actually indicates that the dependency is in a
  112.          nonexistent subdirectory.  */
  113.  
  114.           dep->changed = !dir_file_exists_p (name, "");
  115.           if (dep->changed && *name == '/')
  116.         {
  117.           /* The name is absolute and the directory does not exist.
  118.              This rule can never possibly match, since this dependency
  119.              can never possibly exist.  So just remove the rule from
  120.              the list.  */
  121.           freerule (rule, lastrule);
  122.           --num_pattern_rules;
  123.           goto end_main_loop;
  124.         }
  125.         }
  126.       else
  127.         /* This dependency does not reside in a subdirectory.  */
  128.         dep->changed = 0;
  129.     }
  130.  
  131.       if (ndeps > max_pattern_deps)
  132.     max_pattern_deps = ndeps;
  133.  
  134.       lastrule = rule;
  135.     end_main_loop:
  136.       rule = next;
  137.     }
  138.   
  139.   if (name != 0)
  140.     free (name);
  141. }
  142.  
  143. /* Convert old-style suffix rules to pattern rules.
  144.    All rules for the suffixes on the .SUFFIXES list
  145.    are converted and added to the chain of pattern rules.  */
  146.  
  147. void
  148. convert_to_pattern ()
  149. {
  150.   register struct dep *d, *d2, *newd;
  151.   register struct file *f;
  152.   register char *rulename;
  153.   register unsigned int slen, s2len;
  154.   register char *name, **names;
  155.  
  156.   /* Compute maximum length of all the suffixes.  */
  157.  
  158.   maxsuffix = 0;
  159.   for (d = suffix_file->deps; d != 0; d = d->next)
  160.     {
  161.       register unsigned int namelen = strlen (dep_name (d));
  162.       if (namelen > maxsuffix)
  163.     maxsuffix = namelen;
  164.     }
  165.  
  166.   rulename = (char *) alloca ((maxsuffix * 2) + 1);
  167.  
  168.   for (d = suffix_file->deps; d != 0; d = d->next)
  169.     {
  170.       /* Make a rule that is just the suffix, with no deps or commands.
  171.      This rule exists solely to disqualify match-anything rules.  */
  172.       slen = strlen (dep_name (d));
  173.       name = (char *) xmalloc (1 + slen + 1);
  174.       name[0] = '%';
  175.       bcopy (dep_name (d), name + 1, slen + 1);
  176.       names = (char **) xmalloc (2 * sizeof (char *));
  177.       names[0] = name;
  178.       names[1] = 0;
  179.       create_pattern_rule (names, (char **) 0, 0, (struct dep *) 0,
  180.                (struct commands *) 0, 0);
  181.  
  182.       f = d->file;
  183.       if (f->cmds != 0)
  184.     {
  185.       /* Record a pattern for this suffix's null-suffix rule.  */
  186.       newd = (struct dep *) xmalloc (sizeof (struct dep));
  187.       /* Construct this again rather than using the contents
  188.          of NAME (above), since that may have been freed by
  189.          create_pattern_rule.  */
  190.       newd->name = (char *) xmalloc (1 + slen + 1);
  191.       newd->name[0] = '%';
  192.       bcopy (dep_name (d), newd->name + 1, slen + 1);
  193.       newd->next = 0;
  194.       names = (char **) xmalloc (2 * sizeof (char *));
  195.       names[0] = savestring ("%", 1);
  196.       names[1] = 0;
  197.       create_pattern_rule (names, (char **) 0, 0, newd, f->cmds, 0);
  198.     }
  199.  
  200.       /* Record a pattern for each of this suffix's two-suffix rules.  */
  201.       bcopy (dep_name (d), rulename, slen);
  202.       for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  203.     {
  204.       s2len = strlen (dep_name (d2));
  205.  
  206.       if (slen == s2len && streq (dep_name (d), dep_name (d2)))
  207.         continue;
  208.  
  209.       bcopy (dep_name (d2), rulename + slen, s2len + 1);
  210.       f = lookup_file (rulename);
  211.       if (f == 0 || f->cmds == 0)
  212.         continue;
  213.  
  214.       if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
  215.         /* The suffix rule `.X.a:' is converted
  216.            to the pattern rule `(%.o): %.X'.  */
  217.         name = savestring ("(%.o)", 5);
  218.       else
  219.         {
  220.           /* The suffix rule `.X.Y:' is converted
  221.          to the pattern rule `%.Y: %.X'.  */
  222.           name = (char *) xmalloc (1 + s2len + 1);
  223.           name[0] = '%';
  224.           bcopy (dep_name (d2), name + 1, s2len + 1);
  225.         }
  226.       names = (char **) xmalloc (2 * sizeof (char *));
  227.       names[0] = name;
  228.       names[1] = 0;
  229.       newd = (struct dep *) xmalloc (sizeof (struct dep));
  230.       newd->next = 0;
  231.       /* Construct this again (see comment above).  */
  232.       newd->name = (char *) xmalloc (1 + slen + 1);
  233.       newd->name[0] = '%';
  234.       bcopy (dep_name (d), newd->name + 1, slen + 1);
  235.       create_pattern_rule (names, (char **) 0, 0, newd, f->cmds, 0);
  236.     }
  237.     }
  238. }
  239.  
  240.  
  241. /* Install the pattern rule RULE (whose fields have been filled in)
  242.    at the end of the list (so that any rules previously defined
  243.    will take precedence).  If this rule duplicates a previous one
  244.    (identical target and dependencies), the old one is replaced
  245.    if OVERRIDE is nonzero, otherwise this new one is thrown out.
  246.    When an old rule is replaced, the new one is put at the end of the
  247.    list.  Return nonzero if RULE is used; zero if not.  */
  248.  
  249. int
  250. new_pattern_rule (rule, override)
  251.      register struct rule *rule;
  252.      int override;
  253. {
  254.   register struct rule *r, *lastrule;
  255.   register unsigned int i, j;
  256.  
  257.   rule->in_use = 0;
  258.   rule->terminal = 0;
  259.  
  260.   rule->next = 0;
  261.  
  262.   /* Search for an identical rule.  */
  263.   lastrule = 0;
  264.   for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
  265.     for (i = 0; rule->targets[i] != 0; ++i)
  266.       {
  267.     for (j = 0; r->targets[j] != 0; ++j)
  268.       if (!streq (rule->targets[i], r->targets[j]))
  269.         break;
  270.     if (r->targets[j] == 0)
  271.       /* All the targets matched.  */
  272.       {
  273.         register struct dep *d, *d2;
  274.         for (d = rule->deps, d2 = r->deps;
  275.          d != 0 && d2 != 0; d = d->next, d2 = d2->next)
  276.           if (!streq (dep_name (d), dep_name (d2)))
  277.         break;
  278.         if (d == 0 && d2 == 0)
  279.           /* All the dependencies matched.  */
  280.           if (override)
  281.         {
  282.           /* Remove the old rule.  */
  283.           freerule (r, lastrule);
  284.           /* Install the new one.  */
  285.           if (pattern_rules == 0)
  286.             pattern_rules = rule;
  287.           else
  288.             last_pattern_rule->next = rule;
  289.           last_pattern_rule = rule;
  290.           
  291.           /* We got one.  Stop looking.  */
  292.           goto matched;
  293.         }
  294.           else
  295.         {
  296.           /* The old rule stays intact.  Destroy the new one.  */
  297.           freerule (rule, (struct rule *) 0);
  298.           return 0;
  299.         }
  300.       }
  301.       }
  302.  
  303.  matched:;
  304.  
  305.   if (r == 0)
  306.     {
  307.       /* There was no rule to replace.  */
  308.       if (pattern_rules == 0)
  309.     pattern_rules = rule;
  310.       else
  311.     last_pattern_rule->next = rule;
  312.       last_pattern_rule = rule;
  313.     }
  314.  
  315.   return 1;
  316. }
  317.  
  318.  
  319. /* Install an implicit pattern rule based on the three text strings
  320.    in the structure P points to.  These strings come from one of
  321.    the arrays of default implicit pattern rules.
  322.    TERMINAL specifies what the `terminal' field of the rule should be.  */
  323.  
  324. void
  325. install_pattern_rule (p, terminal)
  326.      struct pspec *p;
  327.      int terminal;
  328. {
  329.   register struct rule *r;
  330.   char *ptr;
  331.  
  332.   r = (struct rule *) xmalloc (sizeof (struct rule));
  333.  
  334.   r->targets = (char **) xmalloc (2 * sizeof (char *));
  335.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  336.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  337.  
  338.   r->targets[1] = 0;
  339.   r->suffixes[1] = 0;
  340.   r->lens[1] = 0;
  341.  
  342.   r->lens[0] = strlen (p->target);
  343.   /* These will all be string literals, but we malloc space for
  344.      them anyway because somebody might want to free them later on.  */
  345.   r->targets[0] = savestring (p->target, r->lens[0]);
  346.   r->suffixes[0] = find_percent (r->targets[0]);
  347.   if (r->suffixes[0] == 0)
  348.     /* Programmer-out-to-lunch error.  */
  349.     abort ();
  350.   else
  351.     ++r->suffixes[0];
  352.  
  353.   ptr = p->dep;
  354.   r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
  355.                                                        sizeof (struct dep)),
  356.                        sizeof (struct dep),
  357.                        1);
  358.  
  359.   if (new_pattern_rule (r, 0))
  360.     {
  361.       r->terminal = terminal;
  362.       r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
  363.       r->cmds->filename = 0;
  364.       r->cmds->lineno = 0;
  365.       /* These will all be string literals, but we malloc space for them
  366.      anyway because somebody might want to free them later.  */
  367.       r->cmds->commands = savestring (p->commands, strlen (p->commands));
  368.       r->cmds->command_lines = 0;
  369.     }
  370. }
  371.  
  372.  
  373. /* Free all the storage used in RULE and take it out of the
  374.    pattern_rules chain.  LASTRULE is the rule whose next pointer
  375.    points to RULE.  */
  376.  
  377. static void
  378. freerule (rule, lastrule)
  379.      register struct rule *rule, *lastrule;
  380. {
  381.   struct rule *next = rule->next;
  382.   register unsigned int i;
  383.  
  384.   for (i = 0; rule->targets[i] != 0; ++i)
  385.     free (rule->targets[i]);
  386.  
  387.   free ((char *) rule->targets);
  388.   free ((char *) rule->suffixes);
  389.   free ((char *) rule->lens);
  390.  
  391.   /* We can't free the storage for the commands because there
  392.      are ways that they could be in more than one place:
  393.        * If the commands came from a suffix rule, they could also be in
  394.        the `struct file's for other suffix rules or plain targets given
  395.        on the same makefile line.
  396.        * If two suffixes that together make a two-suffix rule were each
  397.        given twice in the .SUFFIXES list, and in the proper order, two
  398.        identical pattern rules would be created and the second one would
  399.        be discarded here, but both would contain the same `struct commands'
  400.        pointer from the `struct file' for the suffix rule.  */
  401.  
  402.   free ((char *) rule);
  403.  
  404.   if (pattern_rules == rule)
  405.     if (lastrule != 0)
  406.       abort ();
  407.     else
  408.       pattern_rules = next;
  409.   else if (lastrule != 0)
  410.     lastrule->next = next;
  411.   if (last_pattern_rule == rule)
  412.     last_pattern_rule = lastrule;
  413. }
  414.  
  415. /* Create a new pattern rule with the targets in the nil-terminated
  416.    array TARGETS.  If TARGET_PERCENTS is not nil, it is an array of
  417.    pointers into the elements of TARGETS, where the `%'s are.
  418.    The new rule has dependencies DEPS and commands from COMMANDS.
  419.    It is a terminal rule if TERMINAL is nonzero.  This rule overrides
  420.    identical rules with different commands if OVERRIDE is nonzero.
  421.  
  422.    The storage for TARGETS and its elements is used and must not be freed
  423.    until the rule is destroyed.  The storage for TARGET_PERCENTS is not used;
  424.    it may be freed.  */
  425.  
  426. void
  427. create_pattern_rule (targets, target_percents,
  428.              terminal, deps, commands, override)
  429.      char **targets, **target_percents;
  430.      int terminal;
  431.      struct dep *deps;
  432.      struct commands *commands;
  433.      int override;
  434. {
  435.   register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
  436.   register unsigned int max_targets, i;
  437.  
  438.   r->cmds = commands;
  439.   r->deps = deps;
  440.   r->targets = targets;
  441.  
  442.   max_targets = 2;
  443.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  444.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  445.   for (i = 0; targets[i] != 0; ++i)
  446.     {
  447.       if (i == max_targets - 1)
  448.     {
  449.       max_targets += 5;
  450.       r->lens = (unsigned int *)
  451.         xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
  452.       r->suffixes = (char **)
  453.         xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
  454.     }
  455.       r->lens[i] = strlen (targets[i]);
  456.       r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
  457.             : target_percents[i]) + 1;
  458.       if (r->suffixes[i] == 0)
  459.     abort ();
  460.     }
  461.  
  462.   if (i < max_targets - 1)
  463.     {
  464.       r->lens = (unsigned int *) xrealloc ((char *) r->lens,
  465.                        (i + 1) * sizeof (unsigned int));
  466.       r->suffixes = (char **) xrealloc ((char *) r->suffixes,
  467.                     (i + 1) * sizeof (char *));
  468.     }
  469.  
  470.   if (new_pattern_rule (r, override))
  471.     r->terminal = terminal;
  472. }
  473.  
  474. /* Print the data base of rules.  */
  475.  
  476. void
  477. print_rule_data_base ()
  478. {
  479.   register unsigned int rules, terminal;
  480.   register struct rule *r;
  481.   register struct dep *d;
  482.   register unsigned int i;
  483.  
  484.   puts ("\n# Implicit Rules");
  485.  
  486.   rules = terminal = 0;
  487.   for (r = pattern_rules; r != 0; r = r->next)
  488.     {
  489.       ++rules;
  490.  
  491.       putchar ('\n');
  492.       for (i = 0; r->targets[i] != 0; ++i)
  493.     {
  494.       fputs (r->targets[i], stdout);
  495.       if (r->targets[i + 1] != 0)
  496.         putchar (' ');
  497.       else
  498.         putchar (':');
  499.     }
  500.       if (r->terminal)
  501.     {
  502.       ++terminal;
  503.       putchar (':');
  504.     }
  505.  
  506.       for (d = r->deps; d != 0; d = d->next)
  507.     printf (" %s", dep_name (d));
  508.       putchar ('\n');
  509.  
  510.       if (r->cmds != 0)
  511.     print_commands (r->cmds);
  512.     }
  513.  
  514.   if (rules == 0)
  515.     puts ("\n# No implicit rules.");
  516.   else
  517.     {
  518.       printf ("\n# %u implicit rules, %u", rules, terminal);
  519. #ifndef    NO_FLOAT
  520.       printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
  521. #endif
  522.       puts (" terminal.");
  523.     }
  524.  
  525.   if (num_pattern_rules != rules)
  526.     fatal ("BUG: num_pattern_rules wrong!  %u != %u",
  527.        num_pattern_rules, rules);
  528. }
  529.